<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('主治')" />
    <th:block th:include="include :: bootstrap-editable-css" />
</head>
<body class="gray-bg">

<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="post-form">
                <div class="select-list">
                    <ul>
                        <li>
                            商品码：<input type="text" name="goodscode" th:placeholder="商品码"/>
                    </li>
                       <!-- <li>
                            拼音码：<input type="text" name="plucode" th:placeholder="条码"/>
                        </li>-->
                        <li>
                            商品名称：<input type="text" name="goodsname" th:placeholder="商品名称"/>
                        </li>

                        <li>
                            药品类型：<select name="classname"  id="deviceType">
                            <option value="">---请选择---</option>
                            <option value="OTC呼吸系统">OTC呼吸系统</option>

                            <option value="OTC解热镇痛">OTC解热镇痛</option>

                            <option value="OTC其他类">OTC其他类</option>
                            <option value="OTC其他类（内）">OTC其他类（内）</option>
                            <option value="OTC清热解毒">OTC清热解毒</option>
                            <option value="OTC外用（串味）">OTC外用（串味）</option>
                            <option value="OTC外用药">OTC外用药</option>
                            <option value="OTC五官科">OTC五官科</option>
                            <option value="OTC消化系统">OTC消化系统</option>
                            <option value="参茸">参茸</option>
                            <option value="处方药">处方药</option>
                            <option value="含麻黄碱专柜">含麻黄碱专柜</option>
                            <option value="呼吸系统">呼吸系统</option>
                            <option value="解热镇痛">解热镇痛</option>
                            <option value="九珍堂">九珍堂</option>
                            <option value="日用百货">日用百货</option>
                            <option value="上虹">上虹</option>
                            <option value="食品">食品</option>
                            <option value="外用药">外用药</option>
                            <option value="维生素">维生素</option>
                            <option value="消化系统">消化系统</option>
                            <option value="心血管">心血管</option>
                            <option value="医疗器械">医疗器械</option>
                            <option value="原研药">原研药</option>

                        </select>
                        </li>

                        <li>
                            售价：<input type="text" name="price" th:placeholder="售价"/>
                        </li>

                        <li hidden>
                            <input id="flag3" name="flag3" value=""></input>
                        </li>
<!--                        <li>
                            客户名称：<input type="text" name="companyName"/>
                        </li>-->
                        <!--  <li>
                              岗位状态：<select name="status" th:with="type=${@dict.getType('sys_normal_disable')}">
                              <option value="">所有</option>
                              <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                          </select>
                          </li>-->
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            <!--<a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;保存</a>-->
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
<!--            <a class="btn btn-success" onclick="$.operate.add()" >
                <i class="fa fa-plus"></i> 新增
            </a>
            <a class="btn btn-success" onclick="link()" >
                <i class="fa"></i> 新增药品关系
            </a>
            <a class="btn btn-success" onclick="recommend()" >
                <i class="fa"></i> 新增为推荐
            </a>
            <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="system:post:edit">
                <i class="fa fa-edit"></i> 修改
            </a>
            <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="system:post:remove">
                <i class="fa fa-remove"></i> 删除
            </a>-->
<!--            <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="system:post:export">
                <i class="fa fa-download"></i> 导出
            </a>-->

            <a class="btn btn-info" onclick="$.table.importExcel()" >
                <i class="fa fa-upload"></i> 导入
            </a>
            <a class="btn btn-warning" onclick="$.table.exportExcel()">
                <i class="fa fa-download"></i> 导出
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
<!--    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="post-form1">
                <div class="select-list">
                    <ul>
                        <li>
                            药品信息：<input type="text" name="plucode" th:placeholder="店内码或拼音码"/>
                        </li>
                        &lt;!&ndash;                        <li>
                                                    客户名称：<input type="text" name="companyName"/>
                                                </li>&ndash;&gt;
                        &lt;!&ndash;  <li>
                              岗位状态：<select name="status" th:with="type=${@dict.getType('sys_normal_disable')}">
                              <option value="">所有</option>
                              <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                          </select>
                          </li>&ndash;&gt;
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>-->

        <!--        <div class="btn-group-sm" id="toolbar" role="group">
                    <a class="btn btn-success" onclick="$.operate.add()" >
                        <i class="fa fa-plus"></i> 新增
                    </a>
                    <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="system:post:edit">
                        <i class="fa fa-edit"></i> 修改
                    </a>
                    <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="system:post:remove">
                        <i class="fa fa-remove"></i> 删除
                    </a>
                    <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="system:post:export">
                        <i class="fa fa-download"></i> 导出
                    </a>
                </div>-->

<!--        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table1"></table>
        </div>-->
    </div>
</div>


<th:block th:include="include :: footer" />
<th:block th:include="include :: bootstrap-table-editable-js" />
<th:block th:include="include :: bootstrap-table-export-js" />
<script >
    var ctx = "[[@{/}]]";
</script>
<script th:inline="javascript">
    //  var editFlag = [[${@permission.hasPermi('system:post:edit')}]];
    // var removeFlag = [[${@permission.hasPermi('system:post:remove')}]];
    var datas = [[${@dict.getType('sys_normal_disable')}]];
    var prefix = ctx + "baodao/goodszz";


    $(function() {
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add/{id}",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: ctx + 'baodao/goodszz' + "/export",
            importTemplateUrl: ctx + 'baodao/pay' + "/importTemplate",
            importUrl: ctx + 'baodao/goodszz' + "/importData",
            /*  sortName: "companyId",*/
            modalName: "药品主治",
            // 测试父子表
            detailView: false,
            clickEdit:true,
            onEditableSave: onEditableSave,
           // showExport: true,  //导出
            // 测试新增 查询条件
           /* queryParams: function(params) {
                var curParams = {
                    // 传递参数查询参数
                    pageSize: params.limit,
                    pageNum: params.offset / params.limit + 1,
                    searchValue: params.search,
                    orderByColumn: params.sort,
                    isAsc: params.order,
                    // 新增 flag 为0 展示全部 药品信息，为1 展示药品关联表中已建立关系的药品列表
                    //   flag:  $("#flag").val()
                    flag: 0
                }
                return curParams;
            },*/
            columns: [{
                checkbox: true
            },
                {
                    field: 'goodscode',
                    title: '商品码',
                    width:100
                },
                {
                    field: 'goodsname',
                    title: '名称',
                    width:130
                },
                {
                    field: 'mfrs',
                    title: '生产厂商',
                    width:160

                },
                {
                    field: 'norm',
                    title: '规格',
                    width: 100
                },
                {
                    field: 'price',
                    title: '售价',
                    width: 120,
                    formatter: function(value){
                        if(value == null){
                            return '';
                        }else {
                            return value;
                        }

                    },
                    editable : {
                        type : 'text',
                        title : '售价',
                        mode: "inline",
                        emptytext : "",
                        validate : function(value) {
                            if (value != null && value != '') {
                                // 价格校验
                                var priceReg = /(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/;
                                if (!priceReg.test(value)) {
                                    //  alert("请输入正确的产品价格:整数或者保留两位小数")
                                    return "请输入正确的产品价格:整数或者保留两位小数";
                                }

                                /*                            if (value.length > 200) {
                                return '名称不能超过200个字符';
                            }*/
                                /*                            if (value.length == 0) {
                                                            return '名称不能为空';
                                                        }*/
                            }else
                            {return  ''}

                        }

                    }
                },
                {
                    //https://www.cnblogs.com/landeanfen/p/5821192.html

                    field: 'goodsZZ',
                    title: '主治',
                    width: 200,
                    formatter: function(value){
                        if(value == null){
                            return '';
                        }else {
                            return value;
                        }

                    },
                    editable : {
                        type : 'text',
                        title : '主治',
                        mode: "inline",
                        emptytext : "",
                        validate : function(value) {
                            if (value.length > 200) {
                                return '名称不能超过200个字符';
                            }
/*                            if (value.length == 0) {
                                return '名称不能为空';
                            }*/
                        }
                    }
                }

                ],
/*            onClickCell: function (field, value, row, $element) {
                if(field == "goodsZZ") {

                    $element.attr('contenteditable', true);
                    $element.blur(function () {
                        let index = $element.parent().data('index');
                        let tdValue = $element.html();

                         saveData(index, field, tdValue);
                    })
                }
            },*/
        };


        $.table.init(options);

        $('#bootstrap-table').on( 'click', 'td:has(.editable)', function (e) {
            //e.preventDefault();
           // console.log("点击单元格")
            e.stopPropagation(); // 阻止事件的冒泡行为
            $(this).find('.editable').editable('show'); // 打开被点击单元格的编辑状态
        } );



    });
</script>

<script>
    // 获得所有选中的数据
    function link() {
        // $.table.set();
        var rows = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
        if (rows.length == 0) {
            $.modal.alertWarning("请至少选择一条记录");
            return;
        }
        console.log("已选择数据数量：" + rows.length)
        console.log("已选择数据" + rows.toString())
        let id = rows.toString();
        let url = ctx + 'baodao/demo02/add' + '?id=' + id;
        console.log("弹窗 url" + url)
        var result = url.replace(/#/g, '%23');
        $.modal.openTab("新增药品关系",result);
        // 关闭本窗口
       // var index = ctx + '/baodao/demo02';
        $.modal.close(prefix);
    }

/*    function saveData(index, field, value) {
        $("#bootstrap-table").bootstrapTable('updateCell', {
            index: index,       //行索引
            field: field,       //列名
            value: value        //cell值
        })
    }*/

    function onEditableSave (field, row, rowIndex, oldValue, $el) {
       // alert("字段名：" + field + "，当前值：" + row[field]  + "，旧值：" + oldValue );
       // alert("商品码：" + row.goodscode + "，当前值：" + row[field]  + "，旧值：" + oldValue );
        //更新数据库数据

        console.log(field)
        console.log(row)
        console.log(rowIndex)
        console.log(oldValue)
        console.log($el)
        if(field == 'goodsZZ'){
            // 更新主治信息
        if(row[field] != '待补充') {
            $.ajax({
                type: 'post',
                url: ctx + 'baodao/goodszz/updateGoodsZZ',  // 获取设备历史温度
                timeout: 30000,
                data: {"goodsCode": row.goodscode, "goodsZZ": row[field]},
                dataType: 'json',
                success: function (data) {

                    console.log(data);
                },

                error: function () {
                    alert("调用数据接口失败");
                }
            });
        }
        }

        if(field == 'price'){
            // 更新价格信息
            if(row[field] != '待补充') {
                $.ajax({
                    type: 'post',
                    url: ctx + 'baodao/goodszz/updatePrice',  // 获取设备历史温度
                    timeout: 30000,
                    data: {"goodsCode": row.goodscode, "price": row[field]},
                    dataType: 'json',
                    success: function (data) {

                        console.log(data);
                    },

                    error: function () {
                        alert("调用数据接口失败");
                    }
                });
            }
        }



    }

    /*    function recommend() {
            var rows = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
            if (rows.length == 0) {
                $.modal.alertWarning("请至少选择一条记录");
                return;
            }
            console.log("已选择数据数量：" + rows.length)
            console.log("已选择数据" + rows.toString())
            let id = rows.toString();
            let url = ctx + 'baodao/demo01/edit' + '?id=' + id;
            console.log("弹窗 url" + url)
            var result = url.replace(/#/g, '%23');
            $.modal.openTab("新增为关联",result);
        }*/
    //初始化子表格(无线循环)
    initSubTable = function (index, row, $detail) {

        function queryParams(){
            let tmp = {"goodsCode":row.goodscode};
            return tmp;
        }
        var parentid = row.id;
        var cur_table = $detail.html('<table></table>').find('table');
      //  var cur_table = $detail.html('<table><title>测试</title></table>').find('table');
        $(cur_table).bootstrapTable({
            url: ctx + 'baodao/demo02/queryGoodsLink',
            method: 'post',
            contentType: 'application/x-www-form-urlencoded',
            queryParams:queryParams(),
            //data: {'goodsCode':'401-01-330'},
            ajaxOptions: { id: parentid },
            uniqueId: "id",
            striped: true, //是否显示行间隔色
            pagination: true,//显示分页
            sidePagination: "client",
            pageNumber:1,
            pageSize: 4,
            pageList: [4, 25],
            columns: [
                {
                    field: '',
                    title: '关联药品'
                },
                {
                    field: 'goodscode',
                    title: '内码'
                },
                {
                    field: 'goodsname',
                    title: '名称'
                },
                {
                    field: 'mfrs',
                    title: '厂商'
                },
                {
                    field: 'norm',
                    title: '规格'
                },
/*                {
                    field: 'plucode',
                    title: '关联药品推荐'
                },*/
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        /* actions.push('<a class="btn btn-info  btn-xs ' + addFlag + '" href="javascript:void(0)" onclick="$.operate.add(\'' + row.goodscode + '\')"><i class="fa fa-plus"></i>新增</a> ');*/
                        /*   actions.push('<a class="btn btn-success btn-xs ' + "editFlag" + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.goodscode + '\')"><i class="fa fa-edit"></i>编辑</a> ');*/
                        /* actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.postId + '\')"><i class="fa fa-remove"></i>删除</a>');*/
                        /* actions.push('<a class="btn btn-danger btn-xs ' + "removeFlag" + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.goodscode + '\')"><i class="fa fa-remove"></i>删除</a>');*/
                        return actions.join('');
                    }
                }
            /*    {
                    field: 'mfrs',
                    title: '生产企业'
                },
                {
                    field: 'norm',
                    title: '包装规则'
                },
                {
                    field: 'plucode',
                    title: '条码'
                },*/
            ],
            //无线循环取子表，直到子表里面没有记录
            onExpandRow: function (index, row, $Subdetail) {
                initSubTable(index, row, $Subdetail);
            }
        });
    };


</script>

<!-- 导入区域 -->
<script id="importTpl" type="text/template">
    <form enctype="multipart/form-data" class="mt20 mb10">
        <div class="col-xs-offset-1">
            <input type="file" id="file" name="file"/>
            <div class="mt10 pt5">
                <input type="checkbox" id="updateSupport" name="updateSupport" title="如果登录账户已经存在，更新这条数据。"> 是否更新已经存在的药品主治数据
                &nbsp;	<a onclick="$.table.importTemplate()" class="btn btn-default btn-xs"><i class="fa fa-file-excel-o"></i> 下载模板</a>
            </div>
            <font color="red" class="pull-left mt10">
                提示：仅允许导入“xls”或“xlsx”格式文件！
            </font>
        </div>
    </form>
</script>
</body>
</html>